﻿/*********************************************************************************/
/**    Pattern Preset Color Extractor                                           **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/

using System.Collections.Generic;
using s3pi.Interfaces;
using s3pi.WrapperDealer;
using System.IO;


namespace PatternPresetColorExtractor
{
    /// <summary>
    /// reads resources from a package file
    /// </summary>
    class PackageReader
    {
        #region methods

        /// <summary>
        /// reads the icon und xml resources from a package file
        /// </summary>
        /// <param name="path">path of the package file</param>
        /// <param name="isTemp">is package a temporary file</param>
        /// <returns>pattern object filled with read informations</returns>
        public Pattern read(string path, bool isTemp)
        {
            Pattern pattern = new Pattern(path);

            IPackage                    package     = s3pi.Package.Package.OpenPackage(0, path, false);
            List<IResourceIndexEntry>   resources  = package.GetResourceList;

            foreach (IResourceIndexEntry indexEntry in resources)
            {
                //Get Icon Image from ICON Resource (different res types for small medium, large, very large)
                if (    indexEntry.ResourceType == 0x2E75C764
                     || indexEntry.ResourceType == 0x2E75C765
                     || indexEntry.ResourceType == 0x2E75C766
                     || indexEntry.ResourceType == 0x2E75C767
                    )
                {
                    IResource imageResource = WrapperDealer.GetResource(0, package, indexEntry);
                    ImageResource.ImageResource imageRes = imageResource as ImageResource.ImageResource;
                    pattern.Icon = imageRes.Value;
                }

                // Get pattern xml ressource
                else if (indexEntry.ResourceType == 0x0333406C)
                {
                    IResource   xmlResource = WrapperDealer.GetResource(0, package, indexEntry);
                    Stream      stream      = xmlResource.Stream;
                   
                    pattern.Xml = System.Text.UTF8Encoding.UTF8.GetString(xmlResource.AsBytes);                   
                    
                }
                // Get pattern xml manifest
                else if (indexEntry.ResourceType == 0x73E93EEB)
                {
                    IResource xmlResource = WrapperDealer.GetResource(0, package, indexEntry);
                    Stream stream = xmlResource.Stream;

                    pattern.Manifest = System.Text.UTF8Encoding.UTF8.GetString(xmlResource.AsBytes);

                }

            }

            s3pi.Package.Package.ClosePackage(0, package);

            if (isTemp)
            {
                File.Delete(pattern.Path);
                
            }

            return pattern;
        }

        #endregion
    }
}
